var postfix = 'yqd_';
function put(k, v, t) {
    uni.setStorageSync(k, v)
    var seconds = parseInt(t);
    if (seconds > 0) {
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000 + seconds;
        uni.setStorageSync(postfix + k, timestamp + "")
    } else {
        uni.removeStorageSync(postfix + k)
    }
}


/**
 * 获取缓存
 * @param  {[type]} k   [键名]
 * @param  {[type]} def [获取为空时默认]
 */
function get(k, def) {
    var deadtime = parseInt(uni.getStorageSync(postfix + k))
    if (deadtime) {
        if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
            if (def) {
                return def;
            } else {
                remove(k)  //获取过期直接删除
                return false;
            }
        }
    }
    var res = uni.getStorageSync(k);
    if (res) {
        return res;
    } else {
        if (def == undefined  || def == "") {
            def = false;
        }
        return def;
    }
}

function remove(k) {
    uni.removeStorageSync(k);
    uni.removeStorageSync(postfix + k);
}

/**
 * 清理所有缓存
 * @return {[type]} [description]
 */
function clear() {
    uni.clearStorageSync();
}

export default {
    put,
    get,
    remove,
    clear
}
